
$(function(){ 
    (function(){

        var tools = (function(){
            function setEnv(){
                var host = document.domain;
                var hostArr = host.split(".");
        
                var env_prod= {
                    domain: "//img.mbalib.com",
                    www_domain: "//www.mbalib.com",
                }
        
                var env_dev = {
                    domain: "//img.dev.mbalib.com",
                    www_domain: "//www.dev.mbalib.com",
                }
                var env_test2 = {
                    domain: "//img.test2.mbalib.com",
                    www_domain: "//www.test2.mbalib.com",
                }
                var env_test = {
                    domain: "//static.test.mbalib.com",
                    www_domain: "//www.test.mbalib.com",
                }
                if(hostArr[1] == 'dev'){
                    return env_dev;
                }else if(hostArr[1] == 'test2') {
                    return env_test2;
                }else if(hostArr[1] == 'test'){
                    return env_test;
                }
                return env_prod;
            
            }
            function _getCookie(name)
            {
                var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
                if(arr=document.cookie.match(reg))
                    return unescape(arr[2]);
                else
                    return null;
            }
            function _setCookie(c_name,value,minute,domain)
            {
                if(!minute || minute==0) return;
                
                var exdate=new Date();
                //exdate.setDate(exdate.getDate()+expiredays);
                exdate.setTime(exdate.getTime()+60*1000*minute);
                document.cookie=c_name+ "=" +escape(value)+";path=/;domain="+domain+
                    ((minute==null) ? "" : ";expires="+exdate.toGMTString());
            }
            function _setCookie2(c_name,value,expiredays,domain)
            {
                var exdate=new Date();
                exdate.setDate(exdate.getDate()+expiredays);
                document.cookie=c_name+ "=" +value+";path=/"+
                    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+';domain='+domain;
            }
            function _isPC() {
                var userAgentInfo = navigator.userAgent;
                var Agents = ["Android", "iPhone",
                    "SymbianOS", "Windows Phone",
                    "iPad", "iPod"];
                var flag = true;
                for (var v = 0; v < Agents.length; v++) {
                    if (userAgentInfo.indexOf(Agents[v]) > 0) {
                        flag = false;
                        break;
                    }
                }
                return flag;
            }
            function _isWeixin(){
                if(navigator.userAgent.toLowerCase().indexOf("micromessenger") > 0)
                {
                    return true;
                }
                return false;
            }
            function _nowTime(){
                var mydate = new Date();
                var d = mydate.getDate();
                var m = mydate.getMonth() + 1;
                var y = mydate.getFullYear();
                var h = mydate.getHours();
                var mi = mydate.getMinutes();
                y = y.toString();
                if(m < 10){
                    m = '0' + m.toString();
                }else{
                    m = m.toString();
                }
        
                if(d < 10){
                    d = '0' + d.toString();
                }else{
                    d = d.toString();
                }
                let ymdh = y+m+d+h;
                return { y:y, m:m, d:d, h:h, mi:mi, ymdh: ymdh};
            }
            function _getUrlRelativePath()
            {
                var url = document.location.toString();
                var arrUrl = url.split("//");
        
                var start = arrUrl[1].indexOf("/");
                if(arrUrl[1].indexOf('/#/')!=-1){
                    start = arrUrl[1].indexOf("/#/")+2;
                }
                var relUrl = arrUrl[1].substring(start);
        
                if(relUrl.indexOf("?") != -1){
                    relUrl = relUrl.split("?")[0];
                }
                return relUrl;
            }
            return{
                getCookie: _getCookie,
                setCookie: _setCookie,
                setCookie2: _setCookie2,
                isPC: _isPC,
                isWeixin: _isWeixin,
                nowTime: _nowTime,
                servers: setEnv(),
                ymdh: _nowTime().ymdh,
                getUrlRelativePath: _getUrlRelativePath,
            };
        })();
        
        var plugin =(function(){
            function _randOne(arr){
                if(arr && arr.length){
                    var max = arr.length - 1;
                    var rand = parseInt(Math.random()*(max+1),10);
                    return arr[rand];
                }
            }
            function _entranceSet(type, item) {
                var entrance = {type:type, item:item};
                // sessionStorage.setItem('order_entrance',JSON.stringify(entrance));
                tools.setCookie2('order_entrance',JSON.stringify(entrance),null,'.mbalib.com');
                return true;
            }
            return {
                randOne: _randOne,
                entranceSet: _entranceSet,
            };
        })();


        function loadfile(filename,filetype){

            if(filetype == "js"){
                var fileref = document.createElement('script');
                fileref.setAttribute("type","text/javascript");
                fileref.setAttribute("src",filename);
            }else if(filetype == "css"){
                var fileref = document.createElement('link');
                fileref.setAttribute("rel","stylesheet");
                fileref.setAttribute("type","text/css");
                fileref.setAttribute("href",filename);
            }
            if(typeof fileref != "undefined"){
                document.getElementsByTagName("head")[0].appendChild(fileref);
            }

        }



        function mbalibRecordClick(qs){
            $.ajax({
                url: tools.servers.www_domain+'/appwiki/adstat',
                type:'POST',
                async: false,
                data: qs
            });
        }

        function weixinGeneratescheme(url){
            let openlink = '';
            $.ajax({
                url: tools.servers.www_domain+'/weixin/generatescheme',
                type:'POST',
                async: false,
                dataType: 'json',
                data: {
                    url: encodeURIComponent(url)
                },
                success:function(result){
                    if(result.openlink){
                        openlink = result.openlink;
                    }
                }
            });
            return openlink;
        }

        //实例
        function AdPop(ad, cookiename, from, evts) {
            if(!ad){
                return;
            }

            this.ad_url_type = ad.ad_url_type;
            this.key = ad.key;
            this.img = ad.img;
            this.img2 = ad.img2;
            this.url = ad.url || 'javascript:void(0)';
            this.href = ad.url;

            if(tools.isWeixin() && ad.weixin_url) {
                this.url =  ad.weixin_url || 'javascript:void(0)';
            }
            this.name = ad.name;
            this.needCookie = ad.needCookie || true;
            this.interval = ad.interval;
            this.isadshow = ad.isadshow || tools.getCookie(cookiename);
            this.pushname = cookiename+"_"+from+"_"+ad.name;

            if(ad.type == 'pop')
            {
                console.log(ad);
                console.log(this.isadshow);
               // return;
            }

            this.adHTML = evts.adHTML;
            this.pushShow = function(){
                if(typeof(_hmt) != 'undefined'){
                    _hmt.push(['_trackEvent','ad_show', this.pushname]);
                }
            }
            this.pushClick = function(){
                var arr = this.pushname.split('_');
                var $len = arr.length;
                var type = arr[1];
                if($len > 4){
                    for(var $i = 2;$i< $len-2;$i++){
                        type += '_'+arr[$i];
                    }

                }

                if(this.pushname.indexOf('wiki_left_float')!=-1 ){
                    type = 'wikiLeftFloat';
                    if(typeof statWikiClickLeftAd === 'function' ) {
                       statWikiClickLeftAd();
                    }

                }

                //mbalib 内部统计
                mbalibRecordClick({key: this.key});

                // 记录订单入口函数
                plugin.entranceSet(type, arr[$len-2]);

                //百度统计
                if(typeof(_hmt) != 'undefined')
                {
                    _hmt.push(['_trackEvent','ad_click', this.pushname]);
                }
                return true;
             
            }

            if(!this.isadshow && this.adHTML){
                    this.adHTML();
                    if(evts.clickEl){
                        var _this = this;
                        $(evts.clickEl).bind("click",function(e){
                            _this.pushClick();

                            if(_this.ad_url_type=='xcx'){
                                let wxUrl = weixinGeneratescheme(_this.href);
                                if(wxUrl){
                                    location.href = wxUrl;
                                }
                                return false;
                            }
                        });
                    }
                    if(this.needCookie && this.interval && this.interval>0){
                        let host = document.domain;
                        tools.setCookie(cookiename, 1, this.interval, host);
                    }
                    this.pushShow();
            }

        }

        var url = tools.servers.www_domain+"/services/ad"+"?d="+tools.ymdh;
        if(typeof wfAdRoles != 'undefined'){
            url += "&usergroup="+wfAdRoles;
        }
        var host = document.domain;
        var f = host.split('.');
        var from = f[0];
        var ads = new Object();
        var path = tools.getUrlRelativePath();
        var paths = path.split('/');

        function getAds(callback)
        {
            url += from?"&channel="+from:'';
            $.get(url,function(result){
                if(typeof(result) == 'string')
                {
                    result = eval('(' + result + ')');//兼容低版本jquery 培训频道
                }
                for(var i in result)
                {
                    var val = result[i];
                    var channels = val.ad_channel.split(",");
                    if($.inArray(from, channels)<0) continue;

                    if( from == 'ke' &&  path != '/pc/') {
                        //there are ads only in home
                        continue;
                    }

                    if(!ads[val.ad_type]){
                        ads[val.ad_type] = new Array();
                    }
                    var ad = new Object();
                    ad.key = val.ad_key;
                    ad.name = val.ad_name;
                    ad.url = val.ad_url;
                    ad.weixin_url = val.ad_weixin_url;
                    ad.img = val.ad_image;
                    ad.img2 = val.ad_image2;
                    ad.channel = val.ad_channel;
                    ad.interval = val.ad_interval;
                    ad.type = val.ad_type;
                    ad.ad_url_type = val.ad_url_type;
                    ads[val.ad_type].push(ad);

                }
                if(result && ads){
                    callback && callback(result, ads);
                }
                
            },'json');
        }

        getAds(function(result, ads){
            //全站弹窗
            showPopAd(ads.pop, from);

            //百科手机底部
            showBottomAd(ads.bottom, "ad_bottom");

            //右下角
            showCornerAd(ads.corner, "ad_corner");

            //PC橱窗广告
            showShowcaseAd(ads.web_showcase,'web_showcase');

            if(from == 'www'){
                ad_www(ads)
            }
            if(from == 'wiki'){
                ad_wiki(ads);
            }

            if(from == 'training'){
                ad_training(ads);
            }

            if(from == 'doc'){
                ad_doc(ads);
            }

            if(from == 'news'){
                ad_news(ads);
            }

            //大咖H5开通页
            // showWikiAudio(ads.wiki_audio_h5,'wiki_audio_h5');
        });


        function ad_www(ads){
            //www 首页
            showHomeAd(ads.web);

            //www 首页H5
            showHomeH5Ad(ads.web_h5,'web_h5');

            showAskDetailRightTop(ads.ask_detail_right_top,'ask_detail_right_top');
            showAskCategoryRightTop(ads.ask_category_right_top,'ask_category_right_top');

            if (path == '/s') {
                // console.log(ads.web_search_left);
                showSearchAd(ads.web_search_left);
            }
        }
        function ad_wiki(ads){
            if(wfGetUrlParam('app')==1){

            }else{
                //百科左侧
                showWikiLeft1Ad(ads.wiki_left1, 'ad_wikiLeftMiddle');
                showWikiLeft2Ad(ads.wiki_left2, 'ad_wikiLeftBottom');
                showWikiLeft3Ad(ads.wiki_left3, 'ad_wikiLeftTop');
                showWikiLeftFloat(ads.wiki_left_float,'wiki_left_float');
                showWikiRightTop(ads.wiki_right_top,'wiki_right_top');

                //新版百科广告位
                showNewWikiAd(ads.new_wiki_right1,'new_wiki_right1',"#new_wiki_right_ad1");
                showNewWikiAd(ads.new_wiki_right2,'new_wiki_right2',"#new_wiki_right_ad2");
                showNewWikiAd(ads.new_wiki_right3,'new_wiki_right3',"#new_wiki_right_ad3");
                showNewWikiAd(ads.new_wiki_bottom,'new_wiki_bottom',"#new_wiki_bottom");
            }
            
            if(tools.isPC()){
                // vip开通弹窗
                showWikiVipAd(ads.wiki_vip_pop, 'wiki_vip_pop');

                //新版右侧广告悬浮
                var right_ad = $("#right-ad");
                if(right_ad.length > 0)
                {
                    var right_ad_top = $("#right-ad").offset().top;
                    $(window).scroll(function() {
                        var scroll_top = $(window).scrollTop();
                        if(scroll_top - right_ad_top - 200 > 0)
                        {
                            if(!$("#right-ad").hasClass('right-fix'))
                            {
                                $("#right-ad").addClass('right-fix');
                            }
                        }else
                        {
                            $("#right-ad").removeClass('right-fix');
                        }
                    });
                }
                 
                //已登录不显示复制弹层
                // var login_token = tools.getCookie("wikidbLoginToken");
                // if(login_token){
                //     return;
                // }

                var ad_key = (new Date().getTime())+ Math.floor(Math.random() * 100);
                var ad_url = location.protocol+getServer('www')+"/appwiki/wikicopy?key="+ad_key;

                window.copyEvent ={
                    copyCookiename:'ad_copy_able',//cookie 存在 则 可以复制。cookie有效期1个月
                    isadshow: getCookie('ad_copy_able'),
                    selectionObj : {
                        currentSel: null,
                        getSel: function() {
                            //获取对象
                            if (IEVersion() == "6" || IEVersion() == "7" || IEVersion() == "8") {
                                this.currentSel = document.selection.createRange()
                            } else {
                                var selObj = window.getSelection();
                                var range = null;
                                try {
                                    range = selObj.getRangeAt(0)
                                } catch (rangeError) {}
                                this.currentSel = range
                            }
                        },
                        setSel: function() {
                            //执行选择
                            if (this.currentSel) {
                                if (IEVersion() == "6" || IEVersion() == "7" || IEVersion() == "8") {
                                    this.currentSel.select()
                                } else {
                                    window.getSelection().removeAllRanges();
                                    window.getSelection().addRange(this.currentSel)
                                }
                                this.currentSel = null
                            }
                        }
                    },
                    ad : {
                        // key:ad_key,
                        // url:ad_url,
                    },
                    ad_key:ad_key,
                    ad_url:ad_url,
                    timer:null,//app是否已扫码验证，定时器请求
                };


                //复制弹层广告 dom 创建
                let ap = createPopCopy(ads.copy_pop,copyEvent.copyCookiename);

     
                if (ads && ads.copy_pop) {
                    $(document).on('copy', function (e) {
                        // console.log('copy demand');
                        var txt;
                        if (document.selection) {
                            txt = document.selection.createRange().text
                        } else {
                            txt = window.getSelection() + '';
                        }

                        //光标是不是选中了一个有内容的片段
                        if (txt) {
                            copyEvent.isadshow = getCookie(copyEvent.copyCookiename);
                            if (!copyEvent.isadshow) {
                                //需要App扫码
                                $("#know_copy_pop").next().html('');
                                showCopyPop(ap);
                                return false;
                            } else {
                                return true;
                            }
                        }
                    });
                }
        
            } else {
                showWikiBottomPopUp(ads.wiki_mobile_pop, 'wiki_mobile_pop');
            }

        }
        function ad_training(ads){
            //培训右侧
            showTrainingRightAd(ads.training_right);
        }

        function ad_doc(ads){
            if(!$('#pop_img').length){
                if(paths[1] == "")
                    showPopAd(ads.doc_home_pop,from);
                else if(paths[1] == "view") {
                    showPopAd(ads.doc_view_pop, from);
                    showPopAd(ads.doc_view_delay_pop, from);
                    if($('.report-tab').length){
                        showPopAd(ads.pop_doc_report, from);
                    }
                }
                else if(paths[1] == "userpay" && paths[2] == 'recharge')
                    showPopAd(ads.doc_recharge_pop, from);
                else if(paths[1] == "userpay" && paths[2] == 'member')
                    showPopAd(ads.doc_vip_pop, from);
                else if(paths[1] == "downloadtransition")
                    showPopAd(ads.doc_download_pop,from);
                else if(paths[1] == "user" && !paths[2])
                    showPopAd(ads.doc_person_home_pop, from);
                else if(paths[1] == 'report')
                    showPopAd(ads.pop_doc_report, from);
            }

            showDocVipRightAd(ads.doc_vip_right);

            showDocAd(ads.doc_detail_right);
            // showDocAd(ads.doc_detail_center1);
            // showDocAd(ads.doc_detail_center2);
            // showDocAd(ads.doc_detail_center3);
            // showDocAd(ads.doc_detail_center4);
            // showDocAd(ads.doc_detail_center5);
            showDocAd(ads.doc_tag_right);
            showDocAd(ads.doc_download_right1);
            showDocAd(ads.doc_download_right2);
            showDocAd(ads.doc_download_bottom);
            showDocAd(ads.doc_home_center1);
            showDocAd(ads.doc_home_center2);
            showDocAd(ads.doc_home_center3);
            showDocAd(ads.doc_home_center4);
            showDocAd(ads.doc_tag_bottom);
            showDocAd(ads.report_category_right_1);
            showDocAd(ads.report_category_right_2);
            showDocAd(ads.report_kefu_web, true, false);

            if(tools.isPC()){
                showDocAd_vip(ads.doc_vip_banner);
                showDocFloatRight(ads.doc_float_right);
            }else{
                showDocAd_vip(ads.doc_vip_banner_mobile);
            }
        }


        function ad_news(ads){
           showNewsLeftBottomAd(ads.news_left)
        }

        function showNewsLeftBottomAd(ads){
            if(!ads) return false;
            if(!tools.isPC()) return false;

            var ad = plugin.randOne(ads);
            var cookiename = ad.type;
            console.log(cookiename);
 
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var target = this.href?'_blank':'_self';
                    var str = '<a href="'+this.url+'" id="news_left_adimg" target="'+target+'" ><img style="width:200px;height:264px;" src="'+this.img+'" /></a>';
                    $("#news_left_ad").append(str);
                },
                clickEl: '#news_left_adimg',
            });
        }


        function showBottomAd(ads, cookiename)
        {
            if(!ads) return false;

            if(typeof(isapp) != 'undefined' && isapp == 1) return false;
            if(tools.isPC()) return false;
            if(from == 'www' && (path != '/' && path != '/s')) return false;//主站只支持首页和搜索页面

            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<a href="'+this.url+'" class="app-ad"  id="bottom_img"><img src="'+this.img+'" /><span class="close-ico">x</span></a>';
                    $("body").append(str);
                },
                clickEl: '#bottom_img',
            });

            $(".close-ico").click(function(){
                $(".app-ad").hide();
                return false;
            });
        }

        function showCornerAd(ads, cookiename)
        {
            if(!tools.isPC()) return false;

            var ad = plugin.randOne(ads);
            if(!ad || !ad.url) return false;

            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<div class="ad-br">'+
                    '<a href="javascript:void(0)" class="ad-br-close">关闭</a>'+
                    '<img id="corner_img" src="'+this.img+'">'+
                    '</div>';
                    $("body").append(str);
                },
                clickEl: '#corner_img',
            });

            $(".ad-br-close").click(function(){
                $(".ad-br").hide();
                return false;
            });

            $(".ad-br").click(function(){
                if(ad.url)
                {
                    window.open(ad.url);
                }
            });
        }

        function showPopAd(ads, from)
        {
            if(!tools.isPC() || !ads || ads.length==0) return false;

            var ad = plugin.randOne(ads);
            // 如果为'report'研报类型，需要判断是否为研报页面，如果为才去进行显示全局弹窗，如果不为'report'研报类型，正常显示全局弹窗
            // if((!ad.channel.includes('report') && ad.channel.includes('doc')) && !(paths[1] === "view" && $('.report-tab').length || paths[1] === 'report')) return false;
            if(ad.channel.includes('report')){
                if(!ad.channel.includes('doc') && !(paths[1] === "view" && $('.report-tab').length || paths[1] === 'report')){
                    return false;
                }
            } else if(ad.channel.includes('doc') && (paths[1] === "view" && $('.report-tab').length || paths[1] === 'report')) {
                return false;
            }

            var cookiename = ad.type;
            var ap = new AdPop(ad, cookiename, from, {});
            var _this = ap;
            if(_this.isadshow == 1) return false;
            var adHTML = function(ap){
                // if($('#pop_img').length) return; // 可限制只出现一层广告
                var str = '<div class="wjdc-box">'+
                '<div style="position:relative; height:557px;text-align: center;">'+
                '<img id="pop_img" src="'+ap.img+'" style="max-width: 100%">'+
                // '<a href="javascript:void(0)" class="wjdx-close"></a>'+
                '<svg class="wjdx-close" aria-hidden="true"><use xlink:href="#ic_ic_big_close"></use></svg>'+
                '</div>'+
                '</div>';
                str += '<div id="pop-bg" style="position:fixed;top: 0;left:0;  width: 100%;  height: 110%;  background-color: black;  z-index:9999;  -moz-opacity: 0.7;  opacity:.70;  filter: alpha(opacity=70);"></div>';
                $("body").append(str);
            }

            var timeout = 0;
            if(ad.type == 'doc_view_delay_pop')
            {
                timeout = 15000;
            }
            
            setTimeout(function(){
                adHTML(ap);
                tools.setCookie( cookiename, 1, ad.interval, host);

                if(from == 'doc'){
                    if(typeof statAll_PopWindow_ == 'function'){
                        statAll_PopWindow_(0);
                    }
                }else{
                    _this.pushShow();
                }
                  
                $("#pop_img").bind("click",function(){
                    if(from == 'doc'){
                        if(typeof statAll_PopWindow_ == 'function'){
                            statAll_PopWindow_(1);
                        }
                    }else{
                        _this.pushShow();
                    }
                    _this.pushClick();
                });

                $(document).on('click',".wjdx-close",function(){
                    $(".wjdc-box").hide();
                    $("#pop-bg").hide();
                    return false;
                });

                if(_this.href){
                    $(document).on('click',".wjdc-box",function(){
                        window.open(_this.href);
                    });
                }

            }, timeout);

        }

        function showHomeAd(ads)
        {
            if(!tools.isPC()) return false;
            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, 'ad_web', from, {
                adHTML: function(){
                    var str = '<a href="'+this.url+'" target="_blank" id="web_img"><img  src="'+this.img+'"></a>';
                    $("#ad_640").html(str);
                    $("#ad_640").show();
                },
                clickEl: '#web_img',
            });
        }

        function showSearchAd(ads){
            if(!tools.isPC()) return false;

            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, 'web_search_left', from, {
                adHTML: function(){
                    var str = '<a href="'+ this.url +'" target="'+ (this.url?'_blank':'') +'" id="web_search_left"><img  src="'+this.img+'"></a>';
                    $("#ad_641").html(str);
                    $("#ad_641").show();
                },
                clickEl: '#web_search_left',
            });
        }

        function showHomeH5Ad(ads, cookiename)
        {
            if(tools.isPC()) return false;
            
            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<a href="'+this.url+'" target="_blank" id="'+cookiename+'"><img  src="'+this.img+'"></a>';
                    $("#ad_640").html(str);
                    var ad_width = $("#ad_640").width();
                    var ad_height = ad_width/4;
                    $("#ad_640").height(ad_height+"px");
                    $("#ad_640").show();
                },
                clickEl: '#'+cookiename,
            });

        }

        function showAskCategoryRightTop(ads,cookiename)
        {
            if(!tools.isPC()) return false;
            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<a href="'+ this.url +'" ' + (ad.url ? 'target="_blank"' : '')  + ' id="'+cookiename+'"><img  src="'+this.img+'"></a>';
                    $("#ask_category_right_top").html(str);
                },
                clickEl: '#'+cookiename,
            });

        }

        function showAskDetailRightTop(ads, cookiename)
        {
            if(!tools.isPC()) return false;

            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<a style="display: block;overflow: hidden;margin-bottom: 16px;" class="box-shadow" href="'+this.url +'" ' + (ad.url ? 'target="_blank"' : '')  + ' id="'+cookiename+'"><img  src="'+this.img+'"></a>';
                    $("#ask_right_top").html(str);
                },
                clickEl: '#'+cookiename,
            });

        }

        function showShowcaseAd(ads, cookiename)
        {
            if(!tools.isPC()) return false;
            loadfile(tools.servers.domain+"/common/a_pc/showcase/showcase.css?v=403","css");
            loadfile(tools.servers.domain+"/common/a_pc/showcase/showcase.js?v=520","js");

            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var a_str = '<a class="opred" href="javascript:void(0)" id="'+cookiename+'">';
                    if(ad.url) {
                        a_str = '<a class="opred" target="_blank" href="'+this.url+'" id="'+cookiename+'">';
                    }
                    var str = '<div class="appxb">'+
                        '<div class="appxb-header" id="ad-fold">'+
                        '<img src="'+this.img2+'" img-normal="'+this.img2+'" img-xs="'+this.img2+'" />'+
                        ' </div>'+
                        ' <div class="appxb-con" id="ad-con" style="right:100%">'+
                        '<div class="app-warp">'+
                        ' <div style="width: 1190px;margin: 0 auto;position: relative;">'+a_str+
                        '<img style="width: 100%;" src="'+this.img+'">'+
                        '</a>'+
                        '<a class="warp-close" id="popimg-close"  href="javascript:void(0);" ></a>'+
                        '</div></div>'+
                        '</div>'+
                        '</div>';
                    $("body").append(str);
                },
                clickEl: '#'+cookiename,
            });
        }

        function showNewWikiAd(ads, cookiename, ad_container)
        {
            if(!tools.isPC()) return false;

            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<a href="'+this.url+'" target="_blank"><img  src="'+this.img+'"></a>';
                    $(ad_container).html(str);
                    $(ad_container).show();
                },
                clickEl: ad_container,
            });
        }

        function showWikiVipAd(ads, cookiename)
        {
            if(!ads) return false;
            if(!tools.isPC()) return false;

            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<div class="bg hid" id="vip_popup_img" ><div class="pop-box vip-ad"><img src="'+this.img+'" /><svg class="head-icon" aria-hidden="true"><use xlink:href="#ic_ic_big_close"></use></svg></div></div>';
                    $("body").append(str);
                    $('#vip_popup_img').fadeIn();

                    $('#vip_popup_img img').click(function(){
                        $('#vip_popup_img').fadeOut();
                        var vipPayInstance = VipPay.getInstance();
                        vipPayInstance.popupInit('ad_VIP_pop');
                        vipPayInstance.showVipPop();
                    })

                },
                clickEl: '#vip_popup_img img',
            });

            ap && $('.head-icon').click(function(e){
                e.preventDefault();
                
                $('#vip_popup_img').fadeOut();
            })
        }

        function showWikiBottomPopUp(ads, cookiename) {
            if(!ads) return false;
            if(tools.isPC()) return false;
            
            var ad = plugin.randOne(ads);
            ad.needCookie = false;
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<a href="'+this.url+'" class="popup-ad" id="popup_img" ><img src="'+this.img+'" /><svg class="popup-icon" aria-hidden="true"><use xlink:href="#ic_close_filled_white"></use></svg></a>';
                    $("body").append(str);
                    var setTim = setTimeout(function() {
                        $('.popup-ad').css({'bottom': '16px'});
                        // $('.popup-ad').animate({'top': 'calc(100% - 171.5px - 32px)'},'slow');
                        clearTimeout(setTim);
                    }, 100);
                },
                clickEl: '#popup_img',
            });

            ap && $('.popup-icon').click(function(e){
                e.preventDefault();
                
                tools.setCookie(cookiename,1,ad.interval,host);
                // $('.popup-ad').animate({'top': '100%','opacity': '0'},'slow',function() {
                //     $('.popup-ad').hide();
                // });
                $('.popup-ad').css({'bottom': '-100%'});
                $('.popup-ad')[0].addEventListener('transitionend', function() {
                    $('.popup-ad').hide();
                }, false)
                return false;
            })

        }

        function showWikiLeftFloat(ads, cookiename)
        {
            if(!tools.isPC()) return false;
            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    //百科添加左侧悬浮广告
                    if(document.body.offsetWidth > 975){  
                        $("#wiki-10").html('<a target="_blank" href="'+this.url+'"><img src="'+this.img+'" style="max-width:200px"></a>');

                        var ad_top = $("#left-ad").offset().top;
                        var ad_left = $('#column-one').offset().left;
                        $(window).scroll(function() {
                            ad_top = $("#left-ad").offset().top;
                            var scroll_top = $(window).scrollTop();
                            if(scroll_top - ad_top > 0)
                            {
                                if(!$("#left-ad #wiki-10").hasClass('left-fix'))
                                {
                                    $("#left-ad #wiki-10").addClass('left-fix');
                                    $("#left-ad #wiki-10").css({'left': ad_left});
                                }
                            }else
                            {
                                $("#left-ad #wiki-10").removeClass('left-fix');
                            }
                        });

                        $(window).resize(function(){
                            ad_left = $('#column-one').offset().left;
                            $("#left-ad #wiki-10").css({'left': ad_left});
                        })

                    }
                },
                el: '#wiki-10',

            });


        }

        function showWikiLeft1Ad(ads, cookiename)
        {
            if(!tools.isPC()) return false;

            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<a href="'+this.url+'" target="_blank" id="ad_wikiLeft1"><img  src="'+this.img+'" style="max-width:200px"></a>';
                    $("#wiki-03").html(str);
                    $("#wiki-03").show();
                },
                clickEl: '#ad_wikiLeft1',
            });

        }

        function showWikiLeft2Ad(ads, cookiename)
        {
            if(!tools.isPC()) return false;
            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<a href="'+this.url+'" target="_blank" id="ad_wikiLeft2"><img  src="'+this.img+'" style="max-width:200px"></a>';
                    $("#wiki-05").html(str);
                    $("#wiki-05").show();
                },
                clickEl: '#ad_wikiLeft2',
            });
        }


        function showWikiLeft3Ad(ads, cookiename)
        {
            if(!tools.isPC()) return false;
            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<a href="'+this.url+'" target="_blank" id="ad_wikiLeft3"><img  src="'+this.img+'" style="max-width:200px"></a>';
                    $("#wiki-02").html(str);
                    $("#wiki-02").show();

                },
                clickEl: '#ad_wikiLeft3',
            });
        }

        function showWikiAudio(ads,cookiename)
        {
            
            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    if(ad.url){
                        var str = '<a href="'+this.url+'" target="_blank" id="'+cookiename+'"><img  src="'+this.img+'" width=100%></a>';
                    }else{
                        var str = '<img  src="'+this.img+'" width=100%>';
                    }
                    $("#wiki_audio_h5_img").html(str);

                    $(".pay-head-bg").removeClass("pay-head-bg");
                    $(".userhead-bg").css("margin-top","1.2rem");

                },
                clickEl: '#'+cookiename,
            });
        }

        function showWikiRightTop(ads,cookiename)
        {
            if(!tools.isPC()) return false;
            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<a href="'+this.url+'" target="_blank" id="ad_wikirighttop"><img  src="'+this.img+'" style="max-width:200px"></a>';
                    $("#wiki-01").html(str);
                    $("#wiki-01").show();
                },
                clickEl: '#ad_wikirighttop',
            });
        }

        function showTrainingRightAd(ads)
        {
            if(!tools.isPC()) return false;
            var cookiename = "ad_trainingRight";
            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<a href="'+this.url+'" target="_blank" id="ad_trainingRight"><img  src="'+this.img+'" style="max-width:236px"></a>';
                    $("#ad-E02").html(str);
                    $("#ad-E02").show();
                },
                clickEl: '#ad_trainingRight',
            });
        }

        function showDocVipRightAd(ads)
        {
            if(!tools.isPC()) return false;

            var cookiename = "ad_doc_vip";
            var ad = plugin.randOne(ads);
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var str = '<div class="right-a"><a href="'+this.url+'" target="_blank" id="ad_doc_vip_a"><img src="'+this.img+'"></div>';
                    $("#ad_doc_vip").html(str);
                    $("#ad_doc_vip").show();

                },
                clickEl: '#ad_doc_vip_a',
            });
        }

        function showDocAd(ads,isClass,isPC = true)
        {
            if((!tools.isPC() && isPC) || !ads || ads.length==0) return false;

            var ad = plugin.randOne(ads);
            var cookiename = ad.type;
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var id = cookiename + "_div";
                    var str = '<div class="right-a"><a href="'+this.url+'" target="_blank" class="ad_a"><img src="'+this.img+'"></a></div>';
                    if(!ad.url)
                    {
                        str = '<div class="right-a"><span class="ad_a"><img src="'+this.img+'"></span></div>';
                    }
                    if(isClass){
                        $("."+id).html(str);
                        $("."+id).show();
                    } else {
                        $("#"+id).html(str);
                        $("#"+id).show();
                    }
                },
                clickEl: "#"+cookiename+"_div .ad_a",
            });
        }

        function showDocFloatRight(ads)
        {
            if(!ads || ads.length==0) return false;

            var ad = plugin.randOne(ads);
            var cookiename = ad.type;
            ad.needCookie = false;
            ad.isadshow = false;
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var id = "doc_float_right";
                    var str = '<img class="doc_float_right_img" src="'+this.img+'">';
                    if(ad.url){
                        str = '<a style="width:inherit;height:inherit;" href="'+this.url+'" target="_blank"><img  class="doc_float_right_img" src="'+this.img+'"></a>';
                    }
                    $("."+id).html(str);
                },
                clickEl: ".doc_float_right_img",
            });
        }

        function showDocAd_vip(ads)
        {
            if(!ads || ads.length==0) return false;

            var ad = plugin.randOne(ads);
            var cookiename = ad.type;
            ad.needCookie = false;
            ad.isadshow = true;
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var id = "doc_vip_banner_div";
                    var str = '<div class="vip-banner" ><a href="'+this.url+'" target="_blank" class="ad_a_vip"><img class="vip-banner-img" src="'+this.img+'"></a></div>';
                    if(!ad.url){
                        str = '<div class="vip-banner" ><div class="ad_a_vip"><img  class="vip-banner-img" src="'+this.img+'"></div></div>';
                    }
                    $("#"+id).html(str);
                    $("#"+id).show();
                },
                clickEl: "#doc_vip_banner_div .ad_a_vip",
            });
            
        }

        function createPopCopy(ads, cookiename)
        {
            if(!tools.isPC()) return false;

            var ad = plugin.randOne(ads);
            ad.img = "https://img.mbalib.com/common/a_pc/images/wiki/copy.png";//图片放云端,该图片展示的次数多，减少服务端流量
            ad.needCookie = false;
            ad.isadshow = true;
            var ap = new AdPop(ad, cookiename, from, {
                adHTML: function(){
                    var id = "doc_vip_banner_div";
                    var str = '<div class="vip-banner" ><a href="'+this.url+'" target="_blank" class="ad_a_vip"><img class="vip-banner-img" src="'+this.img+'"></a></div>';
                    if(!ad.url){
                        str = '<div class="vip-banner" ><div class="ad_a_vip"><img  class="vip-banner-img" src="'+this.img+'"></div></div>';
                    }
                    $("#"+id).html(str);
                    $("#"+id).show();
                },
                clickEl: "#doc_vip_banner_div .ad_a_vip",
            });
            if(ap){
                showWikiGlobalAd(ap.pushname, cookiename, ad);
                return ap;
            }
        }

        function showCopyPop(ap)
        {
            if($("#copy_pop_wiki").length > 0)
            {
                if(typeof(statWikiCopyPop) == 'function') {
                    statWikiCopyPop();
                }

                $("#copy_pop_wiki").removeClass('hid');
                $("#bg").show();
                console.log(ap);
                ap.pushShow();
            }
        }


        function showWikiGlobalAd(pushname, cookiename, ad){
            copyEvent.ad = ad;
            copyEvent.ad.key = copyEvent.ad_key;
            copyEvent.ad.url = copyEvent.ad_url;

            var rq_logo = getServer('static')+'/wiki/MBALib/images/64.png';
            var close_img = getServer('static')+'/wiki/MBALibNew/images/ic_close_light.png';

            var html = '<div class="common-Dialog hid" data-name="'+pushname+'" id="copy_pop_wiki" style="width:750px;margin-left:-360px;margin-top:-280px;">';
            html += '<div style="height:375px;border-radius:5px 0;position: relative">' +
                '<a id="copy_pop_close" style="position: absolute;top: -14px;right: -14px;display: block;background: url('+close_img+') no-repeat;width:28px;height:28px;cursor: pointer"></a>'+
                // '<a target="_blank" href="'+url+'">' +
                '<img style="width:100%;max-height:100%;display: block" src="'+(ad.img || ad.img2)+'">' +
                // '</a>' +
                '<div class="rq-code" id="app-scan-code" style="width: 140px;height: 140px;position: absolute;top:140px;right:117px;background: #fff;padding:10px">' +
                '<div class="main"></div>' +
                '<div style="position: absolute;display: block;width: 30px;height: 30px;top:50%;left:50%;border-radius: 4px;background: #fff;margin-top:-20px;margin-left:-20px;overflow: hidden;padding:5px;"><img style="display: block;width: 30px;height: 30px;border-radius: 4px" src="'+rq_logo+'" /></div>' +
                '</div>'+
                '</div>';
            html += '<div class="bottom" style="padding: 50px 0 0 0">'
                + '<div class="bottomBtn" style="width: 40%">'
                + ' <a href="javascript:void(0)" style="margin:0 auto;width:144px;padding:0 10px;float: right;background: #0152D9;color:#fff;border:0;line-height: 39px" class="cbtn-m btnlook5  cancelDialog" id="vip_copy_pop">开通百科VIP免验证</a>'
                + ' <a href="javascript:void(0)" style="margin:0 auto;width:90px;padding:0 10px;float: left;background: #FFA300;color:#fff;border:0;line-height: 39px" class="cbtn-m btnlook5  cancelDialog" id="know_copy_pop">我已验证</a>'
                +'<p style="text-align: center;color:#FF3B30;font-size: 14px;padding:10px 0;margin:0;clear: both;"></p>'
                + '</div>'
                + '</div>';
            html += '</div>';
            $("body").append(html);

            $("#copy_pop_close").click(function(){
                if(typeof(statWikiCopyPopKnowClick) == 'function') {
                    statWikiCopyPopKnowClick();
                }

                $("#copy_pop_wiki").addClass('hid');
                $("#bg").hide();
            });

            $('#vip_copy_pop').click(function () {
                var vipPayInstance = VipPay.getInstance();
                vipPayInstance.popupInit('copy_pop');
                $("#copy_pop_wiki").addClass('hid');
                $("#bg").hide();
                vipPayInstance.showVipPop();
            })

            $("#know_copy_pop").click(function(){
                if($("#copy_pop_wiki").hasClass("hid")){
                    return;
                }
                $("#know_copy_pop").next().html('');

                $.ajax({
                    type:"post",
                    url:getServer('www')+'/appwiki/hasVerify',
                    dataType:"json",
                    data:{key:copyEvent.ad.key},
                    success: function(data){
                        if(data.has_verify==1){	//可以复制
                            //写入cookie，有效期1个月
                            tools.setCookie(window.copyEvent.copyCookiename,1,window.copyEvent.ad.interval,host);

                            if(typeof(statWikiCopyPopScan) == 'function') {
                                statWikiCopyPopScan();
                            }
                            //关闭弹层
                            $("#copy_pop_wiki").addClass('hid');
                            $("#bg").hide();

                            //执行复制命令
                            if (getSelectedContentsPerFrame(window) == "") {
                                copyEvent.SelectionObj.setSel()
                            }
                            document.execCommand("Copy");
                        }else{
                            $("#know_copy_pop").next().html('验证失败，请重新扫码');
                        }
                    }
                });


            });


            var options = {
                render: "canvas",
                ecLevel: 'H',//识别度
                foreground: "#000",
                background: '#FFF',
                // quiet: 0,//边距
                width: 140,//宽度
                height: 140,
                text: ad.url,//二维码内容
                src:rq_logo,
            };
            $('#app-scan-code>.main').empty().qrcode(options);
        }

    })();

});


